package com.cloud.drore.eboss.operation.controller;


import com.cloud.drore.eboos.common.base.PagePo;
import com.cloud.drore.eboos.common.base.ResultMessage;

import com.cloud.drore.eboos.common.entity.base.UserInfo;
import com.cloud.drore.eboos.common.entity.dealer.FinaceSourseDataDealerVo;
import com.cloud.drore.eboos.common.entity.operation.po.FinaceSourseDataCommonPo;


import com.cloud.drore.eboos.common.util.LocalData;
import com.cloud.drore.eboos.common.util.LocalDateAndDateTransformUtil;
import com.cloud.drore.eboss.login.user.Anon;
import com.cloud.drore.eboss.operation.entity.*;

import com.cloud.drore.eboos.common.entity.dealer.FinaceSourseDataDealerPo;
import com.cloud.drore.eboss.operation.entity.po.*;

import com.cloud.drore.eboos.common.entity.supplier.vo.FinaceSupplierIncomeDetailsVo;
import com.cloud.drore.eboos.exception.BusinessException;


import com.cloud.drore.eboss.operation.entity.vo.FinaceSourseDataOperationAndSupplierVo;
import com.cloud.drore.eboss.operation.entity.vo.FundSettlementTotalVo;
import com.cloud.drore.eboss.operation.entity.vo.FundSettlementVo;
import com.cloud.drore.eboss.operation.entity.vo.SupplierSettlementVoucherHistoryVo;
import com.cloud.drore.eboss.operation.service.IBaseDataFeign;

import com.cloud.drore.eboss.operation.service.impl.FinaceFormOperationServiceImpl;


import com.cloud.drore.eboss.operation.service.impl.FinaceSettlementHistoryServiceImpl;
import com.cloud.drore.eboss.operation.service.impl.FinaceSupplierRechargeServiceImpl;
import com.cloud.drore.eboss.supplier.entity.po.FinaceSupplierIncomeDetailsPo;
import com.cloud.drore.eboss.supplier.entity.vo.ProductQueryVo;
import com.cloud.drore.eboss.supplier.service.impl.ProductRelationServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 浙江卓锐科技股份有限公司 版权所有 © Copyright 2018<br>
 *
 * @Description: finace<br>
 * @Project: eboos <br>
 * @CreateDate: Created in 2018/01/24 11:23 <br>
 * @Author: <a href="410508961@qq.com">夏凯</a>
 */
@RestController
@RequestMapping("/finaceOperation")
@Api(value = "/运营商财务资金结算模块", description = "运营商财务管理资金结算模块-夏凯")
public class FinaceOperationFundController {
    @Autowired
    ThreadPoolTaskScheduler threadPoolTaskScheduler;
    @Resource
    FinaceSettlementHistoryServiceImpl finaceSettlementHistoryServiceImpl;
    @Resource
    FinaceFormOperationServiceImpl finaceOperationServiceImpl;
    @Resource
    FinaceSupplierRechargeServiceImpl finaceSupplierRechargeServiceImpl;
    @Resource
    ProductRelationServiceImpl productRelationServiceImpl;
    @Autowired
    IBaseDataFeign iBaseDataFeign;

    @ApiOperation(value = "通过两次的时间获取这段时间内该供应商需要结算的订单（远程调用）")
    @PostMapping(value = "/getFinaceFormList")
    public ResultMessage getFinaceFormList(@RequestBody @Valid List<FinaceSourseDataCommonPo> finaceSourseDataCommonPo) throws BusinessException {

        return finaceOperationServiceImpl.finaceSourseDataOperationList(finaceSourseDataCommonPo);
    }


    @Anon
    @ApiOperation(value = "分销商查询收入报表（远程调用）")
    @PostMapping(value = "/queryFinaceSettlementFormList")
    public ResultMessage<List<FinaceSourseDataDealerVo>> queryFinaceSettlementFormList(@RequestBody @Valid PagePo<FinaceSourseDataDealerPo> page) {

        return finaceSettlementHistoryServiceImpl.queryFinaceSettlementDealerTotalList(page);
    }


    @ApiOperation(value = "通过供应商id查询产品编号（产品下拉框）")
    @GetMapping(value = "/getProductIdList/{id}")
    public ResultMessage<List<ProductQueryVo>> get(@PathVariable("id") Long id) {
        return finaceOperationServiceImpl.findProductNum(id);
    }

    @ApiOperation(value = "test")
    @PostMapping(value = "/test")
    public ResultMessage test(@RequestBody FinaceSettlementFlagOperation finaceSettlementFlagOperation) throws BusinessException {
        // FinaceSettlementHistoryOperation finaceSettlementHistoryOperation = test.buildStage(finaceSettlementFlagOperation.getSupplierId(), finaceSettlementFlagOperation.getAutoSettlementTime());
        finaceSettlementHistoryServiceImpl.addJob(1, 1L);
        return new ResultMessage("success");
    }

    @ApiOperation(value = "供应商自动结算触发事件，页面加载的时候调用改接口，返回false表示已经点击过自动结算，true表示执行自动结算成功")
    @GetMapping(value = "/supplierSettlementFlag/{id}")
    public ResultMessage supplierSettlementFlag(@PathVariable("id") Long id) {
        return ResultMessage.success(finaceSettlementHistoryServiceImpl.queryEarliestOrderFlag(id));
    }

    @Anon
    @ApiOperation(value = "供应商自动结算触发事件，真的去结算")
    @PostMapping(value = "/supplierSettlement")
    public ResultMessage supplierSettlement(@RequestBody @Valid SupplierSettlementDoPo supplierSettlementDoPo) {
        return ResultMessage.success(finaceSettlementHistoryServiceImpl.queryEarliestOrderTime(supplierSettlementDoPo));
    }


    @ApiOperation(value = "通过供应商id和结算期查询运营商的结算记录")
    @PostMapping(value = "/queryFinaceSettlementHistoryTotalList")
    public ResultMessage<List<FinaceSettlementHistoryOperation>> queryFinaceSettlementHistoryTotalList(@RequestBody @Valid PagePo<FinaceSourseOperationTotalPo> page) {
        return finaceSettlementHistoryServiceImpl.queryFinaceSettlementHistoryTotalList(page);
    }


    @ApiOperation(value = "运营商结算记录通过供应商的id查询结算期下拉列表")
    @GetMapping(value = "/selectManage/{id}")
    public ResultMessage<List<FinaceSettlementHistoryOperation>> selectManage(@PathVariable("id") Long id) {
        return ResultMessage.success(finaceSettlementHistoryServiceImpl.queryStageOperationList(id));
    }


    @ApiOperation(value = "供应商进行充值并记录（新增结算记录）")
    @PostMapping(value = "/supplierSettlementVoucher")
    public ResultMessage<List<FinaceSupplierSettlementVoucherHistory>> supplierSettlementVoucher(@RequestBody @Valid FinaceSupplierSettlementVoucherHistory finaceSupplierSettlementVoucherHistory) {
        ResultMessage resultMessage = new ResultMessage();
        LocalDateTime currenDateTime = LocalDateTime.now();
        finaceSupplierSettlementVoucherHistory.setSettlementTime(LocalDateAndDateTransformUtil.localDateTimeToDate(currenDateTime));
        resultMessage.setId(finaceSupplierRechargeServiceImpl.rechargeSupplier(finaceSupplierSettlementVoucherHistory).getId() + "");
        return resultMessage;
    }

    @ApiOperation(value = "通过分销商id和产品编号以及供应商的id（必传）、起止时间（必传）搜索供应商账单（运营商）")
    @PostMapping(value = "/queryFinaceFormListByChoice")
    public ResultMessage<List<FinaceSourseDataOperationAndSupplierVo>> queryFinaceFormListByChoice(@RequestBody @Valid PagePo<FinaceSourseDataOperationPo> page) throws BusinessException {
        return finaceSettlementHistoryServiceImpl.queryFinaceSettlementOperationTotalList(page);
    }

    @ApiOperation(value = "运营商供应商账单查询收入账单明细：supplier_id、distributor_id(选传，有的话从收入报表那边传过来)、order_status、settlement_start_time、settlement_end_time")
    @PostMapping(value = "/getOperationFinaceIncomeDetailList")
    public ResultMessage<List<FinaceSupplierIncomeDetailsVo>> getOperationFinaceIncomeDetailList(@RequestBody @Valid PagePo<FinaceSupplierIncomeDetailsPo> page) throws BusinessException {
        return finaceOperationServiceImpl.feginOperationFinaceIncomeDetailList(page);
    }

    @ApiOperation(value = "资金结算，供应商id作为非毕传字段")
    @PostMapping(value = "/fundSettlement")
    public ResultMessage<List<FundSettlementVo>> fundSettlement(@RequestBody PagePo<FundSettlementPo> page) {
        return finaceSettlementHistoryServiceImpl.queryFundSettlementList(page);
    }

    @ApiOperation(value = "资金结算，三个平台收入")
    @GetMapping(value = "/queryFundSettlement")
    public ResultMessage<FundSettlementTotalVo> queryFundSettlement() {
        return ResultMessage.success(finaceSettlementHistoryServiceImpl.queryFundSettlementFundSettlementTotalVo());
    }

    @ApiOperation(value = "运营商查询收入明细，初始化订单状态列表信息")
    @GetMapping(value = "/initOrderStatusListOperation")
    public ResultMessage<List<DictionaryOrderPo>> initOrderStatusListOperation() {
        return finaceOperationServiceImpl.queryOrderStatusList();
    }


    @ApiOperation(value = "根据供应商的id和结算期的表id查看凭证")
    @PostMapping(value = "/findSuppplierRechargeHistory")
    public ResultMessage<List<SupplierSettlementVoucherHistoryVo>> findSuppplierRechargeHistory(@RequestBody @Valid SupplierRechargeHistoryPo supplierRechargeHistoryPo) {
        return ResultMessage.success(finaceSupplierRechargeServiceImpl.querySuppplierRechargeHistory(supplierRechargeHistoryPo));
    }

    @Anon
    @ApiOperation(value = "远程调用，分销商查询结算报表产品下拉框初始化")
    @GetMapping(value = "/initProductNumDealer")
    public ResultMessage<List<ProductQueryVo>> initProductNumDealer() {
        return productRelationServiceImpl.queryProductNumDealer();
    }

    @ApiOperation(value = "超级管理员删除供应商上传的凭证记录信息(数据行结算期的id 测试用62)")
    @DeleteMapping(value = "/deleteRechargeSupplierHistoryById/{id}")
    public ResultMessage deleteRechargeHistoryById(@PathVariable("id") Long id) throws BusinessException {
        UserInfo currentUser = LocalData.getCurrentUser();
        if (currentUser.getIsAdmin() != 1) {
            throw new BusinessException("对不起，您不是超级管理员用户！");
        }
        if (finaceSupplierRechargeServiceImpl.deleteSupplierRechargeHistoryById(id) < 1) {
            throw new BusinessException("删除供应商充值记录信息失败!");
        }
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setId(id + "");
        return resultMessage;
    }


}